package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-03-24
 * Time: 10:50
 */
// 发布一篇新博客
@WebServlet("/create")
public class CreateBlogServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 约定请求: POST/create ContentType: application/x-www-from-urlencoded
        // title = ?&content=?  标题以及正文均由浏览器自动 urlencode Servlet 也会自动urldecode

        // 约定响应: HTTP/ 1.1 302
        // Location:blog.list.html

        // 约定前端: 点击发布博客时, 将正文以及博客内容带回
        // POST/create ContentType: application/json title = ? content = ? (全文)
        // 约定后端: HTTP/1.1 跳转至个人主页.

        ObjectMapper objectMapper = new ObjectMapper();
        resp.setContentType("application/json; charset = utf8");
        req.setCharacterEncoding("utf8");
        // 1. 检查登录状态, 获取对话
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        if(user == null) {
            // 提示当前未登录. 强制跳转登录
            resp.getWriter().write("{\"issuccess\":false}");
            return;
        }
        // 有会话登录后, 获取前端传来的数据
        // 前端传来的是一个 json 格式字符串, 解析当前字符串
        Blog blog = objectMapper.readValue(req.getInputStream(), Blog.class);
        // 新的 Blog 对象作为容器
        Blog blog1 = new Blog();
        // 更新
        blog1.setUserId(user.getUserId());
        blog1.setTitle(blog.getTitle());
        blog1.setContent(blog.getContent());

        // 新增到数据库
        BlogDao blogDao = new BlogDao();
        System.out.println(blog1);
        blogDao.add(blog1);
        // 返回新增成功状态码
        resp.getWriter().write("{\"issuccess\":true}");


        // 1. 检查登录状态 获取对话以及用户信息
//        HttpSession session = req.getSession(false);
//        User user = (User) session.getAttribute("user");
//        if(session == null || user == null) {
//            resp.setContentType("text/html; charset = utf8");
//            resp.getWriter().write("当前用户未登录!");
//        }
//        // 2. 获取到当前用户写得博客得 标题 正文
//        req.setCharacterEncoding("utf8");
//        String title = req.getParameter("title");
//        String content = req.getParameter("content");
//
//        // 3. 构造 Blog 对象. 插入到数据库中
//        Blog blog = new Blog();
//        blog.setTitle(title);
//        System.out.println(title);
//        blog.setContent(content);
//        blog.setUserId(user.getUserId()); // 谁登录得就是谁是作者
//        BlogDao blogDao = new BlogDao();
//        blogDao.add(blog);
//
//        // 4. 发布成功重定向到主页
//        resp.sendRedirect("blog_list.html");

    }
}
